package com.vancouver.knows.ui.fragment.citydiscount;


import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.business.bean.BusinessInfo;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.util.AppUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Chris Li on 2016-07-21.
 */
public class CityDiscountsView extends LinearLayout {

    private Context mContext;
    private List<BusinessInfo> mBusinessInfoList;
    private List<DiscountViewHolder> mViewHolders = new ArrayList<>();

    public CityDiscountsView(Context context, List<BusinessInfo> businessInfoList) {
        super(context);
        this.mContext = context;
        this.mBusinessInfoList = businessInfoList;
        init();
    }

    private void init() {
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        for (final BusinessInfo businessInfo : mBusinessInfoList) {
            View view = layoutInflater.inflate(R.layout.business_item_layout, this, false);
            DiscountViewHolder viewHolder = new DiscountViewHolder(view);
            mViewHolders.add(viewHolder);
            addView(view);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, CityDiscountDetailActivity.class);
                    intent.putExtra("postId", businessInfo.getId());
                    mContext.startActivity(intent);
                }
            });

            viewHolder.title.setText(businessInfo.getName());
            viewHolder.category.setText(businessInfo.getCategory());
            if (!businessInfo.getImages().isEmpty()) {
                viewHolder.attachmenImagesContainer.setVisibility(VISIBLE);
                viewHolder.singleImage.setVisibility(GONE);
                if (businessInfo.getImages().size() > 2) {
                    Picasso.with(mContext).load(businessInfo.getImages().get(0)).placeholder(R.drawable.no_image_placeholder).centerCrop()
                            .fit()
                            .into(viewHolder.imageOne);
                    Picasso.with(mContext).load(businessInfo.getImages().get(1)).placeholder(R.drawable.no_image_placeholder).centerCrop()
                            .fit()
                            .into(viewHolder.imageTwo);
                    Picasso.with(mContext).load(businessInfo.getImages().get(2)).placeholder(R.drawable.no_image_placeholder).centerCrop()
                            .fit()
                            .into(viewHolder.imageThree);
                } else {
                    viewHolder.attachmenImagesContainer.setVisibility(GONE);
                    viewHolder.singleImage.setVisibility(VISIBLE);
                    Picasso.with(mContext).load(businessInfo.getThumbnail()).placeholder(R.drawable.no_image_placeholder).centerCrop()
                            .fit()
                            .into(viewHolder.singleImage);
                }
            } else {
                if (!StringUtil.isEmptyString(businessInfo.getThumbnail())) {
                    viewHolder.attachmenImagesContainer.setVisibility(GONE);
                    viewHolder.singleImage.setVisibility(VISIBLE);
                    Picasso.with(mContext).load(businessInfo.getThumbnail()).placeholder(R.drawable.no_image_placeholder).centerCrop()
                            .fit()
                            .into(viewHolder.singleImage);
                } else {
                    viewHolder.singleImage.setImageResource(R.drawable.no_image_placeholder);
                }
            }

            viewHolder.ratingBar.setRating((float) businessInfo.getRating());
            viewHolder.ratingBar.setNumStars(5);
            viewHolder.commentCount.setText(String.valueOf(businessInfo.getCommentCount()));
            viewHolder.distance.setVisibility(GONE);

            if (mBusinessInfoList.size() > 1) {
                View divider = new View(mContext);
                divider.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
                divider.setBackgroundColor(ContextCompat.getColor(mContext, R.color.line_color));
                addView(divider);
            }
        }

    }


    class DiscountViewHolder {

        @Bind(R.id.single_image)
        ImageView singleImage;
        @Bind(R.id.title)
        TextView title;
        @Bind(R.id.attachment_imgs_container)
        LinearLayout attachmenImagesContainer;
        @Bind(R.id.post_attachment_img_one)
        ImageView imageOne;
        @Bind(R.id.post_attachment_img_two)
        ImageView imageTwo;
        @Bind(R.id.post_attachment_img_three)
        ImageView imageThree;
        @Bind(R.id.ratingBar)
        RatingBar ratingBar;
        @Bind(R.id.commentCount)
        TextView commentCount;
        @Bind(R.id.distance)
        TextView distance;
        @Bind(R.id.category)
        TextView category;

        public DiscountViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

    }

}
